package net.zdreamer.geracomium.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.geracomium.R;

import net.zdreamer.geracomium.adapter.AssessContenAdapter;
import net.zdreamer.geracomium.entity.AssessInfos;
import net.zdreamer.geracomium.ui.home.DoAssessActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class AssessContentFragment extends Fragment {

    private TextView tv_question;
    private ListView answerlist;
    private AssessContenAdapter mAdapter;
    private String questiontext;
    public JSONArray array;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_assess_content, container,
                false);
    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        init();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        String question = ((DoAssessActivity) getActivity()).questions.get(DoAssessActivity.count);
        try {
            JSONObject jsonObject = new JSONObject(question);
            jsonObject.getString("description");

            questiontext = DoAssessActivity.count + 1 + "、" + jsonObject.getString("description");
            array = new JSONArray(jsonObject.getString("options"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void init() {

        tv_question = (TextView) getActivity().findViewById(R.id.question);
        answerlist = (ListView) getActivity().findViewById(R.id.answerlist);
        tv_question.setText(questiontext);

        AssessInfos assessInfos = new AssessInfos(array);
        // System.out.println("listsize" + assessInfos.assessinfos.size());
        mAdapter = new AssessContenAdapter(getActivity(), assessInfos.assessinfos, R.layout.question_answer);
        answerlist.setAdapter(mAdapter);
        answerlist.setOnItemClickListener(mAdapter);
    }
}
